<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template v-slot:config_form="{row}">
      <text_more :name="row.name" v-model:value="row.value" :placeholder="row.placeholder"></text_more>
    </template>
  </forms>
</template>

<script>
import s94Mjs from "s94-js";
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let type_map = this.field_type(this.field_type.CAN_CONFIG, 'label');
    let forms_config = {
      group: {label:'配置组',name:'group',type:'select_one',config:[],tips:'配置所属分组'},
      name: {label:'配置名',name:'name',type:'text_one',tips:'配置名，字母加数字，获取/设定配置的依据，谨慎命名'},
      is_only: {label:'是否唯一',name:'is_only',type:'radio_switch',config:'否|0,是|1',tips:'唯一配置只能在总站显示和操作，并且所有租户下通用',value:0},
      is_inherit: {label:'是否继承',name:'is_inherit',type:'radio_switch',config:'否|0,是|1',tips:'继承配置在租户设定为空时，会使用总站的配置',value:0},
      tips: {label:'配置说明',name:'tips',type:'text_more',tips:'配置说明，提示'},
      type: {label:'表单组件',name:'type',type:'select_one',config:type_map,tips:'',value:''},
      config: {label:'组件配置',name:'config',type:'config_form',tips:''},
      sort: {label:'排序',name:'sort',type:'text_one',config:'number',tips:'',value:50},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/config/edit?id=`+this.id : '/admin/config/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('config/index', '$refs.list_url.list');
          iframe.hide('config/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    }
  },
  watch: {
    'config.type.value'(v){
      s94Mjs.each(field_type.component_config(v), (v,k)=>{
        this.config.config[k] = v;
      })
    },
    'config.is_only.value'(v, oldV){
      this.config.is_inherit.remove = (v==1);
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/config/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit');
        }else {
          this.$router.push('index');
        }
      })
    }else {
      this.config.type.value = 'text_one';
    }

    //其他初始化操作
    ajax({url:'/admin/config/config_map',cache:true},function(res){
      app.config.group.config = res.data['group'];
      return false
    })
  }
}
</script>
